import { ref, toRefs } from "vue";
import { useDictStore } from "@/store/modules/dict";
import { GetDictData } from "@/api/system/dict";
export function useDict(...args) {
  const res = ref({});
  return (async () => {
    for (const dictType of args) {
      const index = args.indexOf(dictType);
      res.value[dictType] = [];
      //dictType字典名称
      const dicts = useDictStore().getDict(dictType);
      //如果dicts有值，说明仓库中已经存储，直接使用
      if (dicts) {
        res.value[dictType] = dicts;
      } else {
        //如果无值，代表仓库中还未存储，调用获取字典标签的接口，参数dictType代表要查询的字典名称
        let params = {
          dictType: dictType
        }
        const result = await GetDictData(params);
        res.value[dictType] = result.data.map(p => ({
          label: p.dictLabel,
          value: p.dictValue,
          listClass: p.listClass,
          cssClass: p.cssClass
        }))
        useDictStore().setDict(dictType, res.value[dictType]);
      }
    }
    return toRefs(res.value);
  })()
};
